package com.liming.cartype.core.biz;

import com.liming.cartype.api.biz.ICarTypeService;
import com.liming.cartype.api.persistence.impl.CarTypeBeanImpl;
import com.liming.core.util.config.CouchdbConfig;
import org.lightcouch.Attachment;
import org.lightcouch.CouchDbClient;
import org.lightcouch.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 14-4-28.
 */
public class CarTypeServiceImpl implements ICarTypeService{
    private static CouchDbClient dbClient;


    public void init() {
        CouchdbConfig.getCouchdbConfig().setDbName("carinfo");
        dbClient = new CouchDbClient(
                CouchdbConfig.getCouchdbConfig().getDbName(),
                true,
                "http",
                CouchdbConfig.getCouchdbConfig().getHost(),
                CouchdbConfig.getCouchdbConfig().getPort(),
                CouchdbConfig.getCouchdbConfig().getUsername(),
                CouchdbConfig.getCouchdbConfig().getPassword());
    }


    @Override
    public CarTypeBeanImpl addCarType(CarTypeBeanImpl message) {
        Response response = dbClient.save(message);
        CarTypeBeanImpl foo = dbClient.find(CarTypeBeanImpl.class, response.getId());
        return foo;
    }

    @Override
    public CarTypeBeanImpl updateCarType(CarTypeBeanImpl message) {
        Response response = dbClient.update(message);
        CarTypeBeanImpl foo = dbClient.find(CarTypeBeanImpl.class, response.getId());
        return foo;
    }

    @Override
    public CarTypeBeanImpl putCarType(CarTypeBeanImpl message) {
        if (null != message.getRevision()) {
            return updateCarType(message);
        } else {
            return addCarType(message);
        }
    }

    @Override
    public CarTypeBeanImpl findCarType(String id) {
        CarTypeBeanImpl foo = dbClient.find(CarTypeBeanImpl.class, id);
        return foo;
    }

    @Override
    public void deleteCarType(String id) {
        CarTypeBeanImpl foo = dbClient.find(CarTypeBeanImpl.class, id);
        dbClient.remove(foo);
    }

    @Override
    public List<CarTypeBeanImpl> getAllCarType() {
        List resourceList;
        try {
            resourceList = dbClient.view("CarType/findAllCarType").includeDocs(true).query(CarTypeBeanImpl.class);

        } catch (Exception e) {
            return new ArrayList<CarTypeBeanImpl>();
        }
        if (null != resourceList && resourceList.size() <= 0) {
            return new ArrayList<CarTypeBeanImpl>();
        }
        return resourceList;
    }

    @Override
    public String getAttachmentUrl(CarTypeBeanImpl message, String fileName) {
        String attachMentUrl = "http://127.0.0.1:5984/carinfo";
        CarTypeBeanImpl brandBean = message;
        Map<String, Attachment> attachmentMap1 = brandBean.getAttachments();
        for (String key : attachmentMap1.keySet()) {
            if (fileName.equals(key)) {
                attachMentUrl = attachMentUrl + "/" + brandBean.getId() + "/" + fileName;
                break;
            }
        }
        return attachMentUrl;
    }

    @Override
    public void destroy() {
        dbClient.shutdown();
    }
}
